// Package sys
// @Link  https://github.com/bufanyun/hotgo
// @Copyright  Copyright (c) 2025 HotGo CLI
// @Author  Ms <133814250@qq.com>
// @License  https://github.com/bufanyun/hotgo/blob/master/LICENSE
// @AutoGenerate Version 2.16.10
package sys

import (
	"context"
	"hotgo/internal/dao"
	"hotgo/internal/library/hgorm/handler"
	"hotgo/internal/model/input/form"
	"hotgo/internal/model/input/sysin"
	"hotgo/internal/service"

	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"
)

type sSysHotmatch struct{}

func NewSysHotmatch() *sSysHotmatch {
	return &sSysHotmatch{}
}

func init() {
	service.RegisterSysHotmatch(NewSysHotmatch())
}

// Model 热门赛事ORM模型
func (s *sSysHotmatch) Model(ctx context.Context, option ...*handler.Option) *gdb.Model {
	return handler.Model(dao.Hotmatch.Ctx(ctx), option...)
}

// List 获取热门赛事列表
func (s *sSysHotmatch) List(ctx context.Context, in *sysin.HotmatchListInp) (list []*sysin.HotmatchListModel, totalCount int, err error) {
	mod := s.Model(ctx)

	// 字段过滤
	mod = mod.Fields(sysin.HotmatchListModel{})

	// 查询id
	if in.Id > 0 {
		mod = mod.Where(dao.Hotmatch.Columns().Id, in.Id)
	}

	// 查询标题
	if in.Title != "" {
		mod = mod.WhereLike(dao.Hotmatch.Columns().Title, in.Title)
	}

	// 分页
	mod = mod.Page(in.Page, in.PerPage)

	// 排序
	mod = mod.OrderAsc(dao.Hotmatch.Columns().Sort).OrderDesc(dao.Hotmatch.Columns().Id)

	// 查询数据
	if err = mod.ScanAndCount(&list, &totalCount, false); err != nil {
		err = gerror.Wrap(err, "获取热门赛事列表失败，请稍后重试！")
		return
	}
	return
}

// Edit 修改/新增热门赛事
func (s *sSysHotmatch) Edit(ctx context.Context, in *sysin.HotmatchEditInp) (err error) {
	return g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) (err error) {

		// 修改
		if in.Id > 0 {
			if _, err = s.Model(ctx).
				Fields(sysin.HotmatchUpdateFields{}).
				WherePri(in.Id).Data(in).Update(); err != nil {
				err = gerror.Wrap(err, "修改热门赛事失败，请稍后重试！")
			}
			return
		}

		// 新增
		if _, err = s.Model(ctx, &handler.Option{FilterAuth: false}).
			Fields(sysin.HotmatchInsertFields{}).
			Data(in).OmitEmptyData().Insert(); err != nil {
			err = gerror.Wrap(err, "新增热门赛事失败，请稍后重试！")
		}
		return
	})
}

// Delete 删除热门赛事
func (s *sSysHotmatch) Delete(ctx context.Context, in *sysin.HotmatchDeleteInp) (err error) {

	if _, err = s.Model(ctx).WherePri(in.Id).Unscoped().Delete(); err != nil {
		err = gerror.Wrap(err, "删除热门赛事失败，请稍后重试！")
		return
	}
	return
}

// MaxSort 获取热门赛事最大排序
func (s *sSysHotmatch) MaxSort(ctx context.Context, in *sysin.HotmatchMaxSortInp) (res *sysin.HotmatchMaxSortModel, err error) {
	if err = dao.Hotmatch.Ctx(ctx).Fields(dao.Hotmatch.Columns().Sort).OrderDesc(dao.Hotmatch.Columns().Sort).Scan(&res); err != nil {
		err = gerror.Wrap(err, "获取热门赛事最大排序，请稍后重试！")
		return
	}

	if res == nil {
		res = new(sysin.HotmatchMaxSortModel)
	}

	res.Sort = form.DefaultMaxSort(res.Sort)
	return
}

// View 获取热门赛事指定信息
func (s *sSysHotmatch) View(ctx context.Context, in *sysin.HotmatchViewInp) (res *sysin.HotmatchViewModel, err error) {
	if err = s.Model(ctx).WherePri(in.Id).Scan(&res); err != nil {
		err = gerror.Wrap(err, "获取热门赛事信息，请稍后重试！")
		return
	}
	return
}